package com.wxd.config;

import com.wxd.entity.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author wxd
 * @version V1.0
 * @description BeanPostProcessor
 * @date 2022/10/12 15:14
 **/
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {

    /**
     * 该在初始化方法之前调用
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return null;
    }

    /**
     * 该方法再初始化方法之后调用
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof User) {
            //修改注册时间
            ((User) bean).setRegisterDate(new Date());
        }
        return bean;
    }
}
